package com.yao.club.reposittory;

import com.yao.club.entity.dto.GoodsAttr;
import com.yao.club.entity.dto.Message;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
    @Query(value = "SELECT * FROM message m WHERE m.member_id = :memberId " +
            "AND m.create_time = (SELECT MAX(create_time) FROM message WHERE module = m.module AND member_id = :memberId) " +
            "ORDER BY m.create_time DESC", nativeQuery = true)
    List<Message> findLatestMessagesByModule(@Param("memberId") String memberId);

    @Query(value = "SELECT * FROM message WHERE member_id = :memberId AND module = :module ORDER BY create_time DESC", nativeQuery = true)
    List<Message> findByMemberIdAndModule(@Param("memberId") String memberId, @Param("module") int module);

    @Query(value = "SELECT * FROM message WHERE member_id = :memberId AND module = :module ORDER BY create_time DESC \n-- #pageable\n",
            countQuery = "SELECT COUNT(*) FROM message WHERE member_id = :memberId AND module = :module",
            nativeQuery = true)
    Page<Message> findByMemberIdAndModule(@Param("memberId") String memberId, @Param("module") int module, Pageable pageable);
}
